{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Largest Number At Least Twice of Others"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: dominantIndex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #至少是其他数字两倍的最大数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> ，其中总是存在 <strong>唯一的</strong> 一个最大整数 。</p>\n",
    "\n",
    "<p>请你找出数组中的最大元素并检查它是否 <strong>至少是数组中每个其他数字的两倍</strong> 。如果是，则返回 <strong>最大元素的下标</strong> ，否则返回 <code>-1</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [3,6,1,0]\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>6 是最大的整数，对于数组中的其他整数，6 至少是数组中其他元素的两倍。6 的下标是 1 ，所以返回 1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,3,4]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>4 没有超过 3 的两倍大，所以返回 -1 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= nums.length &lt;= 50</code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 100</code></li>\n",
    "\t<li><code>nums</code> 中的最大元素是唯一的</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [largest-number-at-least-twice-of-others](https://leetcode.cn/problems/largest-number-at-least-twice-of-others/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [largest-number-at-least-twice-of-others](https://leetcode.cn/problems/largest-number-at-least-twice-of-others/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[3,6,1,0]', '[1,2,3,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "     a,ans=max(nums),-1\n",
    "     for i,x in enumerate(nums):\n",
    "         if a==x:ans=i\n",
    "         elif x>a //2:return-1\n",
    "     return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "\n",
    "        在一个给定的数组nums中，总是存在一个最大元素 。\n",
    "\n",
    "        查找数组中的最大元素是否至少是数组中每个其他数字的两倍。\n",
    "\n",
    "        如果是，则返回最大元素的索引，否则返回-1。\n",
    "\n",
    "        示例 1:\n",
    "\n",
    "        >>> nums = [3, 6, 1, 0]\n",
    "        >>> Solution().dominantIndex(nums)\n",
    "        1\n",
    "\n",
    "        解释: 6是最大的整数, 对于数组中的其他整数,\n",
    "        6大于数组中其他元素的两倍。6的索引是1, 所以我们返回1.\n",
    "\n",
    "\n",
    "        示例 2:\n",
    "\n",
    "        >>> nums = [1, 2, 3, 4]\n",
    "        >>> Solution().dominantIndex(nums)\n",
    "        -1\n",
    "\n",
    "        解释: 4没有超过3的两倍大, 所以我们返回 -1.\n",
    "\n",
    "\n",
    "        提示:\n",
    "\n",
    "        nums 的长度范围在[1, 50].\n",
    "        每个 nums[i] 的整数范围在 [0, 99].\n",
    "\n",
    "        来源：力扣（LeetCode）\n",
    "        链接：https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others\n",
    "        著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。\n",
    "        \"\"\"\n",
    "\n",
    "        if not nums:\n",
    "            return -1\n",
    "\n",
    "        if len(nums) == 1:\n",
    "            return 0\n",
    "\n",
    "        # # method 1\n",
    "        # hash_tables = {n: i for i, n in enumerate(nums)}\n",
    "        # nums_ = sorted(nums)\n",
    "        # if nums_[-1] >= nums_[-2] * 2:\n",
    "        #     return hash_tables[nums_[-1]]\n",
    "        # else:\n",
    "        #     return -1\n",
    "\n",
    "        first, first_index = -float('inf'), None\n",
    "        second = -float('inf')\n",
    "        for i, n in enumerate(nums):\n",
    "            if n > first:\n",
    "                second = first\n",
    "                first = n\n",
    "                first_index = i\n",
    "            elif n > second:\n",
    "                second = n\n",
    "\n",
    "        if first >= second * 2:\n",
    "            return first_index\n",
    "\n",
    "        return -1\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    import doctest\n",
    "    doctest.testmod(verbose=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "\n",
    "        在一个给定的数组nums中，总是存在一个最大元素 。\n",
    "\n",
    "        查找数组中的最大元素是否至少是数组中每个其他数字的两倍。\n",
    "\n",
    "        如果是，则返回最大元素的索引，否则返回-1。\n",
    "\n",
    "        示例 1:\n",
    "\n",
    "        >>> nums = [3, 6, 1, 0]\n",
    "        >>> Solution().dominantIndex(nums)\n",
    "        1\n",
    "\n",
    "        解释: 6是最大的整数, 对于数组中的其他整数,\n",
    "        6大于数组中其他元素的两倍。6的索引是1, 所以我们返回1.\n",
    "\n",
    "\n",
    "        示例 2:\n",
    "\n",
    "        >>> nums = [1, 2, 3, 4]\n",
    "        >>> Solution().dominantIndex(nums)\n",
    "        -1\n",
    "\n",
    "        解释: 4没有超过3的两倍大, 所以我们返回 -1.\n",
    "\n",
    "\n",
    "        提示:\n",
    "\n",
    "        nums 的长度范围在[1, 50].\n",
    "        每个 nums[i] 的整数范围在 [0, 99].\n",
    "\n",
    "        来源：力扣（LeetCode）\n",
    "        链接：https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others\n",
    "        著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。\n",
    "        \"\"\"\n",
    "\n",
    "        if not nums:\n",
    "            return -1\n",
    "\n",
    "        if len(nums) == 1:\n",
    "            return 0\n",
    "\n",
    "        # # method 1\n",
    "        # hash_tables = {n: i for i, n in enumerate(nums)}\n",
    "        # nums_ = sorted(nums)\n",
    "        # if nums_[-1] >= nums_[-2] * 2:\n",
    "        #     return hash_tables[nums_[-1]]\n",
    "        # else:\n",
    "        #     return -1\n",
    "\n",
    "        first, first_index = -float('inf'), None\n",
    "        second = -float('inf')\n",
    "        for i, n in enumerate(nums):\n",
    "            if n > first:\n",
    "                second = first\n",
    "                first = n\n",
    "                first_index = i\n",
    "            elif n > second:\n",
    "                second = n\n",
    "\n",
    "        if first >= second * 2:\n",
    "            return first_index\n",
    "\n",
    "        return -1\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    import doctest\n",
    "    doctest.testmod(verbose=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        # 找到最大整数和次大整数\n",
    "        maxVal = 0\n",
    "        idx = 0\n",
    "        sndVal = 0\n",
    "        for i in range(len(nums)):\n",
    "            if (nums[i] > maxVal):\n",
    "                sndVal = maxVal\n",
    "                maxVal = nums[i]\n",
    "                idx = i\n",
    "            elif (nums[i] > sndVal):\n",
    "                sndVal = nums[i]\n",
    "        if (maxVal >= 2 * sndVal):\n",
    "            return idx\n",
    "        else:\n",
    "            return -1\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        num_sort = sorted(nums)\n",
    "        if num_sort[-1] < 2*num_sort[-2]: return -1\n",
    "        max_num = num_sort[-1]\n",
    "        return nums.index(max_num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        nums_l = sorted(nums)\n",
    "        maxn = nums_l[-1]\n",
    "        for i in range(len(nums_l) -1):\n",
    "            if nums_l[i] != 0 and maxn // nums_l[i] < 2:\n",
    "                return -1\n",
    "        return nums.index(maxn) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return 0\n",
    "        a, b = -1, 0\n",
    "        for i in range(1, n):\n",
    "            if nums[i] > nums[b]:\n",
    "                a, b = b, i\n",
    "            elif a == -1 or nums[i] > nums[a]:\n",
    "                a = i\n",
    "        return b if nums[b] >= nums[a] * 2 else -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        max_1 = max(nums) \n",
    "        for i , k in enumerate(nums):\n",
    "            if k == max_1:\n",
    "                dic[k] = i\n",
    "        nums.remove(max_1)\n",
    "        for i in nums:\n",
    "            if 2*i > max_1:\n",
    "                return -1\n",
    "        return dic[max_1]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums) -> int:\n",
    "        \n",
    "        if len(nums)==1:\n",
    "            return 0\n",
    "        else:\n",
    "            temp=nums.copy()\n",
    "            p=0\n",
    "            q=p+1\n",
    "            for x in range(len(temp)):\n",
    "                while q<len(temp):\n",
    "                    if temp[p]>temp[q]:\n",
    "                        temp[p],temp[q]=temp[q],temp[p]\n",
    "                        p+=1\n",
    "                        q=p+1\n",
    "                    else:\n",
    "                        p+=1\n",
    "                        q=p+1\n",
    "                p=0\n",
    "                q=p+1\n",
    "            \n",
    "            if temp[-1]>=temp[-2]*2:\n",
    "                return nums.index(temp[-1])\n",
    "            else:\n",
    "                return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        m1, m2, idx = -1, -1, 0\n",
    "        for i, num in enumerate(nums):\n",
    "            if num > m1:\n",
    "                m1, m2, idx = num, m1, i\n",
    "            elif num > m2:\n",
    "                m2 = num\n",
    "        return idx if m1 >= m2 * 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        t = nums.copy()\n",
    "        nums.sort()\n",
    "        if nums[-1] >= 2 * nums[-2]:\n",
    "            return t.index(nums[-1])\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        a = sorted(nums)\n",
    "        if a[-1] >= 2 * a[-2]:\n",
    "            return nums.index(a[-1])\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        maxValue = -1\n",
    "        index = -1\n",
    "        second = -1\n",
    "\n",
    "        for i, num in enumerate(nums):\n",
    "            if num > maxValue:\n",
    "                if maxValue > second:\n",
    "                    second = maxValue\n",
    "                maxValue = num\n",
    "                index = i\n",
    "            elif num > second:\n",
    "                second = num\n",
    "        if maxValue >= 2* second:\n",
    "            return index\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        dict = {}\n",
    "        for i in range(len(nums)):\n",
    "            dict[nums[i]] = i\n",
    "        nums.sort()\n",
    "        if nums[-1] >= nums[-2]*2:\n",
    "            return dict[nums[-1]]\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        num_max = max(nums)\n",
    "        index = -1\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] != num_max:  # 将除最大的数以外的其它数都翻倍\n",
    "                nums[i] *= 2\n",
    "            else:\n",
    "                index = i  # 找出最大数的索引\n",
    "        new_max = max(nums)\n",
    "        return index if new_max <= num_max else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        tmp=sorted(nums,reverse=True)\n",
    "        length = len(nums)\n",
    "        result = 0\n",
    "        if length==1:\n",
    "            return 0\n",
    "        elif length>1 and tmp[1]*2<=tmp[0]:\n",
    "            for i in range(length):\n",
    "                if nums[i]==tmp[0]:\n",
    "                    result = i\n",
    "        else:\n",
    "            result = -1\n",
    "        return result "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        first = second = id = 0\n",
    "        for i, x in enumerate(nums):\n",
    "            if x> first: \n",
    "               first, second=x, first\n",
    "               id = i\n",
    "            elif x> second:\n",
    "                second = x\n",
    "        return id if first >=2* second else -1        \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        first = second = id = 0\n",
    "        for i, x in enumerate(nums):\n",
    "            if x > first:\n",
    "                first, second = x, first\n",
    "                id = i\n",
    "            elif x > second:\n",
    "                second = x\n",
    "        return id if first >= second * 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        numss = sorted(nums)\n",
    "        n = len(numss)-1\n",
    "        if numss[n] >= numss[n-1] *2:\n",
    "            return nums.index(numss[n])\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max_index = nums.index(max(nums))\n",
    "        nums.sort(reverse=True)\n",
    "\n",
    "        max_num = nums[0]\n",
    "        max_num_two = nums[1]\n",
    "\n",
    "        if max_num_two * 2 <= max_num:\n",
    "            return max_index\n",
    "        else:\n",
    "            return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        num=nums.copy()\n",
    "        num.sort()\n",
    "        b=0\n",
    "        a=num[-1]\n",
    "        c=nums.index(a)\n",
    "        for i in range(len(num)-1):\n",
    "            if num[i]==0:\n",
    "                b+=1\n",
    "                continue\n",
    "            else:\n",
    "                if a//num[i]>=2:\n",
    "                    b+=1\n",
    "        if  b==len(num)-1:\n",
    "            return c\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        n=max(nums)\n",
    "        loc=0\n",
    "        judge=True #判定是否有能够大于其他数字两倍的数\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==n:\n",
    "                loc=i\n",
    "            if nums[i]*2>n and nums[i]!=n:\n",
    "                judge=False\n",
    "        return loc if judge else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "\n",
    "        cur = nums.index(max(nums))\n",
    "        nums.sort()\n",
    "        return cur if nums[-1] >= nums[-2] * 2 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "      sort_nums = sorted(nums)\n",
    "      max_num = sort_nums[-1]\n",
    "      if max_num >= 2 * sort_nums[-2]: return nums.index(max_num)\n",
    "      else: return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        m1=m2=-1\n",
    "        t=0\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            if nums[i]>m1:\n",
    "                m2,m1,t=m1,nums[i],i\n",
    "            elif nums[i]>m2:\n",
    "                m2=nums[i]\n",
    "        return t if m1>=m2*2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        tarMax = -inf\n",
    "        tarMax2 = -inf\n",
    "        tarIndex=-1\n",
    "        for index,n in enumerate(nums):\n",
    "            if n > tarMax:\n",
    "                tarMax = n\n",
    "                tarIndex = index\n",
    "        for n in nums:\n",
    "            if n > tarMax2 and n!=tarMax:\n",
    "                tarMax2 = n\n",
    "        if tarMax<2*tarMax2:\n",
    "            return -1\n",
    "        else:\n",
    "            return tarIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        # mx1 = 0\n",
    "        # mx2 = 0\n",
    "        # index = -1\n",
    "        # # 找到最大值和第二大值\n",
    "        # for i in range(len(nums)):\n",
    "        #     if nums[i] > mx1:\n",
    "        #         mx2 = mx1\n",
    "        #         mx1 = nums[i]\n",
    "        #         index = i\n",
    "        #     elif nums[i] > mx2:\n",
    "        #         mx2 = nums[i]\n",
    "        # #\n",
    "        # return index if mx1 >= mx2 * 2 else -1\n",
    "        ns = sorted(nums, reverse=True)\n",
    "        return nums.index(ns[0]) if ns[0] >= ns[1] * 2 else -1\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        m1, m2, idx = -1, -1, 0\n",
    "        for i, num in enumerate(nums):\n",
    "            if num > m1:\n",
    "                m1, m2, idx = num, m1, i\n",
    "            elif num > m2:\n",
    "                m2 = num\n",
    "        return idx if m1 >= m2 * 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        s = sorted(nums)\n",
    "        if s[-1] < s[-2] * 2:\n",
    "            return -1\n",
    "        return nums.index(s[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        import copy\n",
    "        max1 = max(nums)\n",
    "        nums2 = copy.deepcopy(nums)\n",
    "        nums2.remove(max1)\n",
    "        max2 = max(nums2)\n",
    "        \n",
    "        if max1>=2*max2:\n",
    "            return nums.index(max1)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max_n = max(nums)\n",
    "        for i, v in enumerate(nums):\n",
    "            if v == max_n:\n",
    "                nums[i] = max_n / 2\n",
    "                if max(nums) == max_n / 2:\n",
    "                    return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        mav = 0\n",
    "        for i,c in enumerate(nums):\n",
    "            if c >= 2*mav:\n",
    "                mav = c\n",
    "                ans = i\n",
    "            elif c > mav:\n",
    "                mav = c\n",
    "                ans = -1\n",
    "            elif c > mav/2:\n",
    "                ans = -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        one=max(nums[0],nums[1])\n",
    "        idx=0 if nums[0]>nums[1] else 1\n",
    "        two=min(nums[0],nums[1])\n",
    "        for i in range(2,len(nums)):\n",
    "            if nums[i]>one:\n",
    "                one,two=nums[i],one\n",
    "                idx=i\n",
    "                \n",
    "            elif nums[i]>two:\n",
    "                two=nums[i]\n",
    "        return idx if one>=2*two else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max1 = max2 = idx = -1\n",
    "        for i, v in enumerate(nums):\n",
    "            if v > max1:\n",
    "                max2 = max1\n",
    "                max1 = v\n",
    "                idx = i\n",
    "            elif v > max2:\n",
    "                max2 = v\n",
    "        return idx if max1 >= max2 * 2 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max1 = max2 = idx = -1\n",
    "        for i, v in enumerate(nums):\n",
    "            if v > max1:\n",
    "                max2 = max1\n",
    "                max1 = v\n",
    "                idx = i\n",
    "            elif v > max2:\n",
    "                max2 = v\n",
    "        return idx if max1 >= max2 * 2 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        if nums[0] > nums[1]:\n",
    "            maximum = [nums[1],nums[0]]\n",
    "            location = 0\n",
    "        else:\n",
    "            maximum = [nums[0],nums[1]]\n",
    "            location = 1\n",
    "        n = len(nums)\n",
    "        for i in range(2,n):\n",
    "            if nums[i] > maximum[1]:\n",
    "                location = i\n",
    "                maximum[0] = maximum[1]\n",
    "                maximum[1] = nums[i]\n",
    "            elif nums[i] > maximum[0]:\n",
    "                if nums[i] > 50:\n",
    "                    return -1                \n",
    "                maximum[0] = nums[i]\n",
    "        if maximum[0] * 2 <= maximum[1]:\n",
    "            return location\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max1 = max2 = idx = -1\n",
    "        for i, v in enumerate(nums):\n",
    "            if v >= max1:\n",
    "                max2 = max1\n",
    "                max1 = v\n",
    "                idx = i\n",
    "            elif v >= max2:\n",
    "                max2 = v\n",
    "        return idx if max1 >= max2 * 2 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans = -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == mx:\n",
    "                ans = i\n",
    "                continue\n",
    "            if x * 2 > mx: return -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max_n = max(nums)\n",
    "        for i, v in enumerate(nums):\n",
    "            if v == max_n:\n",
    "                nums[i] = max_n / 2\n",
    "                if max(nums) == max_n / 2:\n",
    "                    return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        tarMax = -inf\n",
    "        tarMax2 = -inf\n",
    "        tarIndex=-1\n",
    "        for index,n in enumerate(nums):\n",
    "            if n > tarMax:\n",
    "                tarMax = n\n",
    "                tarIndex = index\n",
    "        for n in nums:\n",
    "            if n > tarMax2 and n!=tarMax:\n",
    "                tarMax2 = n\n",
    "        if tarMax<2*tarMax2:\n",
    "            return -1\n",
    "        else:\n",
    "            return tarIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        res = float(\"-inf\")\n",
    "\n",
    "        flag = True\n",
    "        for i in range(len(nums)):\n",
    "            res = max(nums[i],res)\n",
    "        print(res)\n",
    "        for i in nums:\n",
    "            if i == res:\n",
    "                continue\n",
    "            else:\n",
    "                if 2*i > res:\n",
    "                    flag = False\n",
    "        if flag:\n",
    "            for i in range(len(nums)):\n",
    "                if nums[i] == res:\n",
    "                    return i\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "         max = 0\n",
    "         for x in nums:\n",
    "             if x > max:\n",
    "                max = x\n",
    "         for x in nums:\n",
    "             if max != x:\n",
    "                if x * 2 > max : return -1\n",
    "         res = -1\n",
    "         n = len(nums)\n",
    "         for i in range(n):\n",
    "             x = nums[i]\n",
    "             if max == x:\n",
    "                res = i\n",
    "         return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        l1=nums[:]\n",
    "        nums.sort()\n",
    "        if nums[-1]>=nums[-2]*2:\n",
    "            for i in l1:\n",
    "                if i==nums[-1]:\n",
    "                    return l1.index(i)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        nums2=nums.copy()\n",
    "        nums2.sort()\n",
    "        if((nums2[-2]<<1)>nums2[-1]):\n",
    "            return -1\n",
    "        else:\n",
    "            max1=nums2[-1]\n",
    "            for i in range(len(nums)):#记录最大值下标\n",
    "                if(nums[i]==max1):\n",
    "                    return i\n",
    "        \n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        maxval = 0\n",
    "        index = -1\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] > maxval:\n",
    "                maxval = nums[i]\n",
    "                index = i\n",
    "        for num in nums:\n",
    "            if num != maxval and num * 2 > maxval:\n",
    "                return -1\n",
    "        return index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        temp=nums[:]\n",
    "        nums.sort()\n",
    "        return temp.index(nums[-1])if nums[-1]>=nums[-2]*2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        first=-1\n",
    "        first_index=-1\n",
    "        second=-1\n",
    "        for i in range(len(nums)):\n",
    "            n=nums[i]\n",
    "            if n>first:\n",
    "                second=first\n",
    "                first=n\n",
    "                first_index=i\n",
    "            elif n>second:\n",
    "                second=n\n",
    "        if first>=2*second:\n",
    "            return first_index\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max_num = max(nums)\n",
    "        for i in nums:\n",
    "            if i > max_num // 2 and i != max_num:\n",
    "                return -1\n",
    "        return nums.index(max_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        temp1 = -float(\"inf\")\n",
    "        temp2 = -float(\"inf\")\n",
    "        ind = 0\n",
    "        for index,item in enumerate(nums):\n",
    "            if item>temp2:\n",
    "                temp1 = temp2\n",
    "                temp2 = item\n",
    "                ind = index\n",
    "            elif item>temp1:\n",
    "                temp1 = item\n",
    "        if temp2 >= 2*temp1:\n",
    "            return ind\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        first_max = -1\n",
    "        second_max = -2\n",
    "        flag = -1\n",
    "        for i,num in enumerate(nums):\n",
    "            if num > first_max:\n",
    "                second_max = first_max\n",
    "                first_max = num\n",
    "                flag = i\n",
    "            elif num > second_max:\n",
    "                second_max = num\n",
    "                \n",
    "        if first_max >= second_max*2:\n",
    "            return(flag)\n",
    "        else:\n",
    "            return(-1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max_1,max_2,j=nums[0],0,0\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i] > max_1:\n",
    "                max_2=max_1\n",
    "                max_1=nums[i]\n",
    "                j=i\n",
    "            elif nums[i] > max_2:\n",
    "                max_2=nums[i]\n",
    "        if max_1 >= 2*max_2:\n",
    "            return j\n",
    "        else:\n",
    "            return -1\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        a=max(nums)\n",
    "        x=nums.index(a)\n",
    "        del nums[x]\n",
    "        b=max(nums)\n",
    "        if b==0 or a/b>=2:\n",
    "            return x\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max1 = max2 = idx = -1\n",
    "        for i, v in enumerate(nums):\n",
    "            if v >= max1:\n",
    "                max2 = max1\n",
    "                max1 = v\n",
    "                idx = i\n",
    "            elif v >= max2:\n",
    "                max2 = v\n",
    "        return idx if max1 >= max2 * 2 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        m1 = m2 = idx = -1\n",
    "        for i,num in enumerate(nums):\n",
    "            if num > m1:\n",
    "                m1, m2, idx = num, m1, i\n",
    "            elif num > m2:\n",
    "                m2 = num\n",
    "        return idx if m1 >= 2 * m2 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max_num = max(nums)\n",
    "        max_num_index = nums.index(max_num)\n",
    "        nums.remove(max_num)\n",
    "        second_max_num = max(nums)\n",
    "\n",
    "        if max_num >= second_max_num*2:\n",
    "            return max_num_index\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        sorted_nums = sorted(nums)\n",
    "        if sorted_nums[-2] == 0:\n",
    "            return nums.index(max(nums))\n",
    "        else:\n",
    "            return nums.index(max(nums)) if sorted_nums[-1] // sorted_nums[-2] >= 2 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        m1 = 0\n",
    "        m2 = 0\n",
    "        index = 0\n",
    "        for i, num in enumerate(nums):\n",
    "            if num > m1:\n",
    "                m2 = m1\n",
    "                m1 = num\n",
    "                index = i\n",
    "            elif num > m2:\n",
    "                m2 = num\n",
    "        # print(m1, m2)\n",
    "        return index if m1 >= m2 * 2 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        # 方法一：最大值 >= 2 倍次大值\n",
    "        first = second = id = 0#id是下标\n",
    "        for i, x in enumerate(nums):\n",
    "            if x > first:\n",
    "                first, second = x, first\n",
    "                id = i\n",
    "            elif x > second:\n",
    "                second = x\n",
    "        \n",
    "        return id if first >= 2 * second else -1\n",
    "        # 方法二：求最大值\n",
    "        ans = max_ = -1        \n",
    "        for i, x in enumerate(nums):  \n",
    "            if x >= max_ * 2: ans = i # 当前的 max_ 对当前的 x 是满足条件的，先保存下来，但不一定是最终值。\n",
    "            elif x > max_ // 2: ans = -1 # 说明 max_ 不符合条件，先记 ans = -1           \n",
    "            max_ = max(max_, x) \n",
    "        return ans\n",
    "        # 方法三：最大值函数\n",
    "        max_, ans = max(nums), -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if max_ == x: ans = i\n",
    "            elif x > max_ // 2: return -1\n",
    "        return ans\n",
    "        # 方法四：离线排序\n",
    "        q = sorted(range(len(nums)), key=lambda i:nums[i])\n",
    "        return -1 if nums[q[-2]]*2 > nums[q[-1]] else q[-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        max1 = max2 = idx = -1\n",
    "        for i, v in enumerate(nums):\n",
    "            if v > max1:\n",
    "                max2 = max1\n",
    "                max1 = v\n",
    "                idx = i\n",
    "            elif v > max2:\n",
    "                max2 = v\n",
    "        return idx if max1 >= max2 * 2 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dominantIndex(self, nums: List[int]) -> int:\n",
    "        a=max(nums)\n",
    "        b=nums.index(a)\n",
    "        s=sorted(nums)\n",
    "        c=s[len(s)-2]\n",
    "        if a>=2*c:\n",
    "            return b\n",
    "        else:\n",
    "            return -1\n",
    "        \n",
    "       "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
